package org.shs.waste.action;

import static org.shs.common.config.SystemInitializer.app_msgs;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.shs.common.config.BaseDispatchAction;
import org.shs.common.constants.AppConstants;
import org.shs.waste.form.WasteTypeForm;
import org.shs.waste.model.WasteTypeModel;
import org.shs.waste.service.WasteService;

/**
 * Handles the user interaction coming in from the waste type related screen
 * <br/>
 * <br/><b>Created:</b>&nbsp;&nbsp; 19-Nov-2008 23:53:03
 *
 * @author anirvan
 *
 */
public class WasteTypeAction extends BaseDispatchAction
{
   
   /**
    * The service class instance for this module
    */
   private static final WasteService serviceCls = new WasteService();
   
   /**
    * Retrieves the various waste-types registered with the system.
    * @param mapping The action mapping instance
    * @param form  The (action) form bean associated
    * @param request The servlet HTTP Request object 
    * @param response The servlet HTTP response object
    * @return The forward mapping to be looked up in the 
    *         struts-config file.
    */
   public ActionForward initWasteTypes(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
   throws Exception
   {
      String sForward = app_msgs.getMessage("screen.waste-main");
      WasteTypeForm formData = (WasteTypeForm)form;
      
      // TODO : Pagination to be done
      request.setAttribute("wasteTypeList", 
                           serviceCls.getWasteTypes(formData.getWasteTypeSrch()));
      
      return mapping.findForward(sForward);
   }
   
   /**
    * Retrieves the details about a particular waste type.
    * @param mapping The action mapping instance
    * @param form  The (action) form bean associated
    * @param request The servlet HTTP Request object 
    * @param response The servlet HTTP response object
    * @return The forward mapping to be looked up in the 
    *         struts-config file.
    */
   public ActionForward getWasteDetails(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
   throws Exception
   {
      String sForward = app_msgs.getMessage("screen.waste-view");
      WasteTypeForm formData = (WasteTypeForm)form;
      
      // Retrieve the waste type details
      request.setAttribute("wasteType", 
                           serviceCls.getWasteTypeDetails(formData
                                                            .getWasteTypeId()));
      
      return mapping.findForward(sForward);
   }
   
   /**
    * Retrieves the details about a particular waste type for the edit screen.
    * @param mapping The action mapping instance
    * @param form  The (action) form bean associated
    * @param request The servlet HTTP Request object 
    * @param response The servlet HTTP response object
    * @return The forward mapping to be looked up in the 
    *         struts-config file.
    */
   public ActionForward editWasteType(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
   throws Exception
   {
      String sForward = app_msgs.getMessage("screen.waste-add-edit");
      WasteTypeForm formData = (WasteTypeForm)form;
      
      // Retrieve the waste type details
      request.setAttribute("wasteType", 
                           serviceCls.getWasteTypeDetails(formData
                                                            .getWasteTypeId()));
      // Also fetch the list of all aggregate waste types
      request.setAttribute("aggrWasteTypes", serviceCls.getAggrWasteTypes());
      
      return mapping.findForward(sForward);
   }
   
   /**
    * Retrieves the data to display the add screen.
    * @param mapping The action mapping instance
    * @param form  The (action) form bean associated
    * @param request The servlet HTTP Request object 
    * @param response The servlet HTTP response object
    * @return The forward mapping to be looked up in the 
    *         struts-config file.
    */
   public ActionForward addWasteType(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
   throws Exception
   {
      String sForward = app_msgs.getMessage("screen.waste-add-edit");
      
      WasteTypeForm formData = (WasteTypeForm) form;
      formData.setWasteTypeDesc(formData.getWasteTypeSrch());
      // Also fetch the list of all aggregate waste types
      request.setAttribute("aggrWasteTypes", serviceCls.getAggrWasteTypes());
      
      return mapping.findForward(sForward);
   }
   
   /**
    * Saves the waste type data into the database.
    * @param mapping The action mapping instance
    * @param form  The (action) form bean associated
    * @param request The servlet HTTP Request object 
    * @param response The servlet HTTP response object
    * @return The forward mapping to be looked up in the 
    *         struts-config file.
    */
   public ActionForward saveWasteType(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
   throws Exception
   {
      String sForward = app_msgs.getMessage("screen.waste-add-edit");
      WasteTypeForm formData = (WasteTypeForm)form;
      WasteTypeModel wasteDetails = null;
      
      formData.setUserId(userData.getId());
      if (formData.getWasteTypeId() == null 
               || formData.getWasteTypeId().length() == 0)
      {
         // Insert the new waste type details
         wasteDetails = serviceCls.addWasteTypeDetails(formData);
      }
      else
      {
         // Update the existing waste type details
         wasteDetails = serviceCls.editWasteTypeDetails(formData);
         
      }
      // Check if all the tasks actually got done properly
      if (wasteDetails != null)
      {
         wasteDetails.setMessage(AppConstants.SUCCESS_MSG);
      }
      else
      {
         wasteDetails = new WasteTypeModel();
         wasteDetails.setMessage(AppConstants.FAILURE_MSG);
      }
      
      // Set the waste details as the request attrib
      request.setAttribute("wasteType", wasteDetails);
      // Also fetch the list of all aggregate waste types
      request.setAttribute("aggrWasteTypes", serviceCls.getAggrWasteTypes());
      
      
      return mapping.findForward(sForward);
   }
   
   /**
    * Deletes the waste type data from the database.
    * @param mapping The action mapping instance
    * @param form  The (action) form bean associated
    * @param request The servlet HTTP Request object 
    * @param response The servlet HTTP response object
    * @return The forward mapping to be looked up in the 
    *         struts-config file.
    */
   public ActionForward delWasteTypes(ActionMapping mapping, 
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response)
   throws Exception
   {
      String sForward = app_msgs.getMessage("screen.waste-main");
      WasteTypeForm formData = (WasteTypeForm)form;
      boolean delStatus = false;
      
      // Delete the selected waste types
      delStatus = serviceCls.delWasteTypeDetails(formData);
      // Retrieve the list of waste types
      request.setAttribute("wasteTypeList", serviceCls.getWasteTypes(null));
      // Set the delete status for appropriate message
      if (delStatus)
      {
         request.setAttribute(AppConstants.SUCCESS_MSG, 
                              app_msgs.getMessage("success.del.wasteType"));
      }
      else
      {
         request.setAttribute(AppConstants.FAILURE_MSG, 
                              app_msgs.getMessage("err.del.wasteType"));
      }
      
      
      return mapping.findForward(sForward);
   }
     
}
